# -*- coding: utf-8 -*-
import sqlite3
import re
import datetime
import time


def convert_time_web(str_time):
    time_tamp = time.mktime(time.strptime(str_time, '%Y-%m-%d %H:%M:%S'))
    time_array = time.localtime(time_tamp)
    other_time = time.strftime("%Y年%m月%d日", time_array)
    return other_time

def convert_time(str_time):
    pub_time = str(datetime.datetime.now().year) + '年' + str_time
    array = time.strptime(pub_time, u"%Y年%m月%d日")
    return time.strftime("%Y年%m月%d日", array)


def is_same_date(a, b):
    try:
        a1, a2 = re.findall(r'\d{1,2}', a)
        b1, b2 = re.findall(r'\d{1,2}', b)
        if int(a1) == int(b1) and int(a2) == int(b2):
            return True
        return
    except ValueError as e:
        pass
        # print(a, 'too many values to unpack')


class Profile:
    def __init__(self):
        # 指定抓取日期
        self.today = datetime.date.today()
        yesterday = self.today + datetime.timedelta(days=-1)  # 减去一天
        self.year = yesterday.strftime('%Y年')
        yesterday = yesterday.strftime('%m月%d日')
        # 指定日期
        # self.todayDate = '05月06日'  # 注意日期格式
        self.todayDate = yesterday
        # 文件名
        self.filename = 'Download/' + self.todayDate + 'file.xls'


class TeambitionConnect:
    def __init__(self):
        pass

    def __enter__(self):
        self.conn = sqlite3.connect('Teambition.db')
        self.cursor = self.conn.cursor()
        return self

    def inset_data(self, Date, Name, Task, Description, Duration):
        sql = """INSERT INTO TeambitionUpdateIinfo (Date,Name,Task,Description,Duration) 
        VALUES ('{}', '{}', '{}', '{}', '{}' );""".format(
            Date, Name, Task, Description, Duration)
        self.cursor.execute(sql)
        self.conn.commit()

    def select_data(self, date1, date2):
        sql = """SELECT Id,Date,Name,Task,Description,Duration from 
        TeambitionUpdateIinfo where Date >='{}' and Date <= '{}';""".format(
            date1, date2)

        return [{'Id': row[0], 'DateTime': row[1], 'Name': row[2], 'Task': row[3], 'Description': row[4],
                 'Duration': row[5]} for row in self.cursor.execute(sql)]

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.conn.close()
